package com.td.client.states;

import java.io.IOException;
import java.net.Socket;

import mdes.slick.sui.Button;
import mdes.slick.sui.Display;
import mdes.slick.sui.Label;
import mdes.slick.sui.TextField;
import mdes.slick.sui.Window;
import mdes.slick.sui.event.ActionEvent;
import mdes.slick.sui.event.ActionListener;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import com.td.client.ClientWindow;
import com.td.core.communication.Communication;
import com.td.core.communication.obj.PlayerInfo;


//TODO
/*
 * GUI jest ekstremalnie proste -> w swoim czasie zrobi sie je przyjemne dla oka
 * 
 * 
 */
public class StartState extends State{
	
	private Display display;
	
	public StartState(ClientWindow clientWindow, GameContainer gameContainer) {
		super(clientWindow, gameContainer);
		
		buildGUI();
	
	
	}

	@Override
	public void render(GameContainer gc, Graphics g) throws SlickException {
		
		display.render(gc, g);

	}


	@Override
	public void update(GameContainer gc, int delta) {

		display.update(gc, delta);
		
	}
	
	private void buildGUI() {
		
		display = new Display(gameContainer);
		Window window = new Window();
		window.setOpaque(true);
		window.setBackground(Color.lightGray);
		
		window.setSize(800, 700);
		window.setLocation(0, 0);
		window.setAlwaysOnTop(true);
		display.add(window);
		
		Label login = new Label("login:");
		login.setLocation(250, 300);
		login.setSize(100, 30);
		window.add(login);
		
		Label server = new Label("server:");
		server.setLocation(250, 330);
		server.setSize(100, 30);
		window.add(server);
		
		TextField userNameField = new TextField(8);
		userNameField.setLocation(360, 300);
		window.add(userNameField);
		
		TextField serverField = new TextField(15);
		serverField.setLocation(360, 330);
		window.add(serverField);
		
		Label label = new Label();
		label.setLocation(300, 390);
		label.setSize(250, 60);
		window.add(label);	
		
		Button startButton = new StartButton("Start game", userNameField, serverField, label);
		startButton.setLocation(375, 360);
		startButton.setSize(80, 20);
		window.add(startButton);	
	}
	
	private class StartButton extends Button {
		
		public StartButton(String text, final TextField userNameField, 
				final TextField serverField, final Label label) {
			super(text);
			
			addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					
					String userName = userNameField.getText();
					PlayerInfo pi = new PlayerInfo();
					pi.setName(userName);
					mainGame.getPreGameInfo().setMyPlayer(pi);

					try {
						mainGame.setCommunication(new Communication(
								new Socket(serverField.getText(), 5077), mainGame));
						label.setText("");
						label.updateAppearance();
					} catch (IOException e) {
						label.setText("Cannot connect to the server. Check if IP is correct.");
						label.updateAppearance();
					}
					
					//tymczasowo
					
					if(mainGame.getCommunication() != null) {
						mainGame.getCommunication().start();
						//mainGame.getCommunication().send(mainGame.getPreGameInfo().getMyPlayer().getCopy());
						mainGame.setState(Type.WAIT);
					}
				}
			});
		}
	}
	
}
